package com.shirley.pers.zhufengfmvi;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;

import com.shirley.pers.zhufengfmvi.fragments.CustomListenFragment;
import com.shirley.pers.zhufengfmvi.fragments.DiscoveryFragment;
import com.shirley.pers.zhufengfmvi.fragments.DownloadListenFragment;
import com.shirley.pers.zhufengfmvi.fragments.PersonalFragment;
import com.shirley.pers.zhufengfmvi.media.PlayService;
import com.shirley.pers.zhufengfmvi.utils.HttpUtils;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private DiscoveryFragment mDiscoveryFragment;
    private CustomListenFragment mCustomListenFragment;
    private DownloadListenFragment mDownloadListenFragment;
    private PersonalFragment mPersonalFragment;

    private Fragment[] mFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //-----------------------------
//        Intent intent = new Intent(this, PlayService.class);
//        startService(intent);

        //-----------------------------

        //采用Fragment现实和隐藏的方式，来管理第一季Fragment
        //默认添加到容器中，之后，切换RadioButton,进行现实和隐藏
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        mDiscoveryFragment = new DiscoveryFragment();
        mCustomListenFragment = new CustomListenFragment();
        mDownloadListenFragment = new DownloadListenFragment();
        mPersonalFragment = new PersonalFragment();
        mFragments = new Fragment[4];
        mFragments[0] = mDiscoveryFragment;
        mFragments[1] = mCustomListenFragment;
        mFragments[2] = mDownloadListenFragment;
        mFragments[3] = mPersonalFragment;

        for (Fragment fragment : mFragments) {
            transaction.add(R.id.main_fragment_container, fragment);
            transaction.hide(fragment);
        }
        //显示第一个
        transaction.show(mFragments[0]);
        transaction.commit();

        //  ---------------------------------

        RadioGroup tabBar = (RadioGroup) findViewById(R.id.main_tab_bar);
        if (tabBar != null) {
            //默认Discovery被checked
            tabBar.check(R.id.main_tab_item_discovery);
            tabBar.setOnCheckedChangeListener(this);
        }

        //测试
//        Thread thread = new Thread(){
//            @Override
//            public void run() {
//                byte[] data = HttpUtils.doGet("http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-f6&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.1.7.1");
//                if (data != null) {
//                    String str = new String(data);
//                    Log.d(TAG, "Get: "+str);
//                }
//            }
//        };
//        thread.start();
    }

    private AlertDialog mExitDialog;

    /**
     * Take care of popping the fragment back stack or finishing the activity
     * as appropriate.
     */
    @Override
    public void onBackPressed() {
        if (mExitDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("温馨提示").setMessage("确认退出珠峰FM吗？")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton("退出", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            Intent intent = new Intent(MainActivity.this, PlayService.class);
                            stopService(intent);
                            finish();
                        }
                    })
                    .setNegativeButton("最小化", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }
                    });
            mExitDialog = builder.create();
            mExitDialog.show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mExitDialog = null;
    }

    /**
     * <p>Called when the checked radio button has changed. When the
     * selection is cleared, checkedId is -1.</p>
     *
     * @param group     the group in which the checked radio button has changed
     * @param checkedId the unique identifier of the newly checked radio button
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        //第一步：隐藏所有的Fragment
        for (Fragment fragment : mFragments) {
            transaction.hide(fragment);
        }
        //第二部：现实切换的Fragment
        Fragment fragment = null;
        switch (checkedId) {
            case R.id.main_tab_item_discovery:
                fragment = mFragments[0];
                break;
            case R.id.main_tab_item_custom:
                fragment = mFragments[1];
                break;
            case R.id.main_tab_item_download:
                fragment = mFragments[2];
                break;
            case R.id.main_tab_item_personal:
                fragment = mFragments[3];
                break;
        }
        if (fragment != null) {
            transaction.show(fragment);
        }
        transaction.commit();
    }
}
